package com.example.demo.service.impl;

import com.example.demo.mapper.QuestionMapper;
import com.example.demo.model.Question;
import com.example.demo.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public Question findById(Integer questionId) {
        return questionMapper.findById(questionId);
    }

    @Override
    public List<Question> findAll() {
        return questionMapper.findAll();
    }
    
    @Override
    public List<Question> search(String keyword) {
        return questionMapper.search(keyword);
    }
    
    @Override
    @Transactional
    public Question save(Question question) {
        // 数据验证
        validateQuestion(question);
        questionMapper.insert(question);
        return question;
    }
    
    @Override
    @Transactional
    public Question update(Question question) {
        // 数据验证
        validateQuestion(question);
        questionMapper.update(question);
        return question;
    }
    
    @Override
    @Transactional
    public Boolean deleteById(Integer questionId) {
        return questionMapper.delete(questionId) > 0;
    }

    /**
     * 验证题目数据的有效性
     * @param question 题目数据
     */
    private void validateQuestion(Question question) {
        if (question == null) {
            throw new IllegalArgumentException("题目数据不能为空");
        }
        if (question.getSection() == null || question.getSection() < 1 || question.getSection() > 4) {
            throw new IllegalArgumentException("分区必须在1-4之间");
        }
        if (question.getNumber() == null || question.getNumber() < 1) {
            throw new IllegalArgumentException("题号必须大于0");
        }
        if (question.getDescription() == null || question.getDescription().trim().isEmpty()) {
            throw new IllegalArgumentException("题目内容不能为空");
        }
        if (question.getOptionA() == null || question.getOptionA().trim().isEmpty()) {
            throw new IllegalArgumentException("选项A不能为空");
        }
        if (question.getOptionB() == null || question.getOptionB().trim().isEmpty()) {
            throw new IllegalArgumentException("选项B不能为空");
        }
        if (question.getAffectsA() == null || question.getAffectsA().trim().isEmpty()) {
            throw new IllegalArgumentException("选项A的影响维度不能为空");
        }
        if (question.getAffectsB() == null || question.getAffectsB().trim().isEmpty()) {
            throw new IllegalArgumentException("选项B的影响维度不能为空");
        }
        
        // 验证维度值是否符合规范
        String affectsA = question.getAffectsA().trim();
        String affectsB = question.getAffectsB().trim();
        
        // 验证维度值长度
        if (affectsA.length() != 1 || affectsB.length() != 1) {
            throw new IllegalArgumentException("维度值必须是单个字母");
        }
        
        // 验证A选项和B选项不能选择相同的维度
        if (affectsA.equals(affectsB)) {
            throw new IllegalArgumentException("A选项和B选项不能选择相同的维度");
        }
        
        // 验证维度值是否合法
        String validDimensions = "EINFSTJP";
        if (!validDimensions.contains(affectsA) || !validDimensions.contains(affectsB)) {
            throw new IllegalArgumentException("维度值必须是E,I,N,F,S,T,J,P中的一个");
        }
        
        // 验证维度值是否与分区匹配
        validateDimensionForSection(question.getSection(), affectsA, affectsB);
    }
    
    /**
     * 验证维度值是否与分区匹配
     */
    private void validateDimensionForSection(Integer section, String affectsA, String affectsB) {
        String[] validPairs = {
            "EI",  // 第1分区：外向-内向
            "SN",  // 第2分区：感觉-直觉
            "TF",  // 第3分区：思考-情感
            "JP"   // 第4分区：判断-知觉
        };
        
        String validPair = validPairs[section - 1];
        if (!validPair.contains(affectsA) || !validPair.contains(affectsB)) {
            throw new IllegalArgumentException(
                String.format("第%d分区的维度值必须是%s中的一个", section, validPair)
            );
        }
    }
}